//
//  ExitController.m
//  敢唆嘛!
//
//  Created by lanou on 15/11/26.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "ExitController.h"
#import "Define.h"
#import "Device.h"
#import "ExitCell.h"
#import "ExitModel.h"
#import "ChangePassWordController.h"
@interface ExitController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableView; //tableView属性

@property (nonatomic,retain)NSMutableArray *modelArray; //装载所有model

@property (nonatomic,retain)UIButton *delegateButton; //清除缓存按钮

@property (nonatomic,retain)UIView *smailView; //点击清除展现的view

@property (nonatomic,retain)UIView *placeView; //点击清除后覆盖的背景视图
@end
static NSString *string = @"cell"; //cell的标记
@implementation ExitController

//创建所有model
-(void)creatExitModel{
    self.modelArray = [NSMutableArray array ];
    NSArray *array = @[@"修改密码", @"清除缓存", @"关于我们"];
    for (int i =0; i<3; i++) {
        ExitModel *model = [[ExitModel alloc]init];
        model.title = array[i];
        [self.modelArray addObject:model];
    }
    
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建左上角返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, KSWidth / 18, KSWidth / 18);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-unie61e"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBackBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftBackBtn;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, KSWidth-20, 50*6)];    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    //添加数据
    [self creatExitModel];
    
    //注册cell
    [self.tableView registerClass:[ExitCell class] forCellReuseIdentifier:string];
    
    //创建一个退出登陆的anniu
    UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    exitButton.frame = CGRectMake(KSWidth/4, KSHeight/4*3, KSWidth/2, 60);
    
    [exitButton.layer setCornerRadius:10];
    
    exitButton.backgroundColor = [UIColor redColor];
    
    [exitButton setTitle:@"退出登陆" forState:UIControlStateNormal];
    
    [exitButton addTarget:self action:@selector(exitButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:exitButton];
    
}

// 返回按钮触发
- (void)backBtnAction:(UIButton *)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}

#pragma mark--返回自定义cell--
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExitCell *cell = [tableView dequeueReusableCellWithIdentifier:string forIndexPath:indexPath];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    ExitModel *model = self.modelArray[indexPath.row];
    
    cell.titleLabel.text = model.title;
    
    //清除缓存行
    if (indexPath.row ==1) {
        //添加一个清除缓存按钮
        self.delegateButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
        self.delegateButton.frame = CGRectMake(KSWidth/4*3-20, 10, KSWidth/6, 25);
        
        [self.delegateButton setTitle:@"清除" forState:UIControlStateNormal];
        
        [self.delegateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.delegateButton.layer setCornerRadius:10];
        
        self.delegateButton.layer.borderWidth = 1;
        
        [self.delegateButton addTarget:self action:@selector(delegateAction) forControlEvents:UIControlEventTouchUpInside];
        
        [cell addSubview:_delegateButton];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}
#pragma mark--点击cell触发的事件--
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0){
        
        ChangePassWordController *changeVC = [[ChangePassWordController alloc]init];
        
        //跳入修改密码界面
        [self.navigationController pushViewController:changeVC animated:YES];
    }
//    else if (indexPath.row == 2) {
//        [self ideaToZMS];
//    }
    else if(indexPath.row == 2 ){
        [self informationChange:@"zms组合是很棒的,请多支持哟!"];
        
    }
    
}

#pragma mark --清除缓存按钮触发的事件--
-(void)delegateAction{
    
    self.smailView = [[UIView alloc]initWithFrame:CGRectMake(50, KSHeight/5*1, KSWidth-100, 0)];
    self.smailView.backgroundColor = [UIColor whiteColor];
    
    self.placeView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.placeView.backgroundColor = [UIColor grayColor];
    self.placeView.alpha = 0.5;
    [self.view addSubview:_placeView];
    
    //清除缓存
    [[SDImageCache sharedImageCache] cleanDisk];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, KSWidth-100, 30)];
    [UIView animateWithDuration:0.7 animations:^{
        
        label.text = @"缓存已清除";
        
        label.textAlignment = NSTextAlignmentCenter;
        
        self.smailView.frame =CGRectMake(50, KSHeight/5*1, KSWidth-100, 100);
        
        [self.view addSubview:self.smailView];
    } completion:^(BOOL finished) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(50, 40, 30, 30);
        button.backgroundColor = [UIColor redColor];
        [button setImage:[UIImage imageNamed:@"yes.png"] forState:UIControlStateNormal];
        [self.smailView addSubview:button];
       [self.smailView addSubview:label];
    }];
    
    //一秒后自动取消小视图
    [self performSelector:@selector(deleteSmailView) withObject:nil afterDelay:3];
   
}

#pragma mark--更新 关于我们  系统推送 --
-(void)informationChange:(NSString *)string{
    self.smailView = [[UIView alloc]initWithFrame:CGRectMake(50, KSHeight/5*1, KSWidth-100, 0)];
    self.smailView.backgroundColor = [UIColor whiteColor];
    
    self.placeView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.placeView.backgroundColor = [UIColor grayColor];
    self.placeView.alpha = 0.5;
    [self.view addSubview:_placeView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, KSWidth-100, 30)];
    [UIView animateWithDuration:0.7 animations:^{
        
        label.text = string;
        
        label.textAlignment = NSTextAlignmentCenter;
        
        self.smailView.frame =CGRectMake(50, KSHeight/5*1, KSWidth-100, 100);
        
        [self.view addSubview:self.smailView];
    } completion:^(BOOL finished) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.smailView.frame.size.width/2-15, 10, 30, 30);
        button.backgroundColor = [UIColor redColor];
        [button setImage:[UIImage imageNamed:@"yes.png"] forState:UIControlStateNormal];
        [self.smailView addSubview:button];
        [self.smailView addSubview:label];
    }];
    
    //一秒后自动取消小视图
    [self performSelector:@selector(deleteSmailView) withObject:nil afterDelay:3];

}


-(void)ideaToZMS{
    
    self.smailView = [[UIView alloc]initWithFrame:CGRectMake(50, KSHeight/5*1, KSWidth-100, 0)];
    self.smailView.backgroundColor = [UIColor whiteColor];
    self.smailView.alpha = 0.7;
    
    self.placeView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.placeView.backgroundColor = [UIColor grayColor];
    self.placeView.alpha = 0.5;
    [self.view addSubview:_placeView];
    
    UITextView *textField = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, KSWidth-100, 70)];
    [UIView animateWithDuration:0.7 animations:^{
        
        textField.backgroundColor = [UIColor grayColor];
        
        textField.alpha = 1;
        
        self.smailView.frame =CGRectMake(50, KSHeight/5*1, KSWidth-100, 100);
        
        [self.view addSubview:self.smailView];
    } completion:^(BOOL finished) {
        
        NSArray *array = @[@"确定", @"取消"];
        for (int i = 0; i<2; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(self.smailView.frame.size.width/2-60+i*80, textField.frame.size.height, 50, 30);
            button.backgroundColor = [UIColor greenColor];
            
            [button setTitle:array[i] forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(samilAction:) forControlEvents:UIControlEventTouchUpInside];
            
            button.tag = 100+i*100;
            
             [self.smailView addSubview:button];
        }
        
        [self.smailView addSubview:textField];
    }];
   
    
    
}
#pragma mark--自动取消小视图的方法--
-(void)deleteSmailView{
    [self.placeView removeFromSuperview];
    
    [self.smailView removeFromSuperview];
    
}

#pragma mark -- 小视图的按钮触发事件--
-(void)samilAction:(UIButton *)btn{
    
    //点击小视图的取消按钮
    if (btn.tag == 200) {
        [self deleteSmailView];
        
    }
    //点击确定按钮
    else{
        [self deleteSmailView];
        [self informationChange:@"我们会认真听取您的建议"];
    }
    
}

#pragma mark--退出登陆的按钮触发的事件--
-(void)exitButton{
    
    //取消缓存的用户账号
    [BmobUser logout];
    
    NSString *string = @"用户名";
    UIImageView *imageView =  [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    imageView.image = [UIImage imageNamed:@"user.png"];

    //判断block实现 是否存在 如果不存在 相当于为nil 不进条件进行回调;
    if (self.block != nil) {
        
        self.block(string);
        self.herderBlock(imageView);
    }
    
    [self.navigationController popViewControllerAnimated:NO];
}
#pragma mark--返回cell的高度--
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
